package com.example.demo.commom;

import org.springframework.util.DigestUtils;

import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2024-04-22
 * Time:14:08
 */
public class Password {
    // 密码组成
    public static String password(String pwd, String salt){
        // 密码1
        String saltpwd = DigestUtils.md5DigestAsHex((salt + pwd).getBytes());
        // 最终密码
        return salt + "$" + saltpwd;
    }
    //生成密码
    public static String slatPwd(String pwd){
        //生成盐值
        String salt = UUID.randomUUID().toString().replace("-","");
        return password(pwd, salt);
    }
    //确认密码
    public static boolean checkPwd(String inputPwd, String FinalPwd){
        //获取盐值
        String salt = FinalPwd.split("\\$")[0];
        //加密
        String pwd = password(inputPwd,salt);
        //对比
        return pwd.equals(FinalPwd);
    }
}
